package com.TexasHoldem;

import com.TexasHoldem.RpcInterface.rpc.TexasHoldemService;
import com.TexasHoldem.RpcInterface.rpc.TexasHoldemService.Response;
import com.google.protobuf.BlockingRpcChannel;
import com.google.protobuf.RpcController;
import com.google.protobuf.ServiceException;
import com.googlecode.protobuf.socketrpc.RpcChannels;
import com.googlecode.protobuf.socketrpc.SocketRpcController;
import com.googlecode.protobuf.socketrpc.SocketRpcProtos.ProtocolMessage;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;


/**
 * @author su
 * 这里是心跳包实现,主要是处理网上突然断线,但是网络异常未必弹出,比如说突然断了WIFI
 * 如果channel 出现 ClosedChannelException异常心跳包是不会触发的
 *
 */
public class TexasHoldemHeartHandle extends IdleStateHandler{

	private int heartnum = 0;
	private int preheartnum = -1;
	
	public TexasHoldemHeartHandle(int readerIdleTimeSeconds,
			int writerIdleTimeSeconds, int allIdleTimeSeconds) {
		super(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds);
		// TODO Auto-generated constructor stub
		
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		// TODO Auto-generated method stub
		
		if(msg instanceof ProtocolMessage)
		{
			ProtocolMessage protocolmsg = (ProtocolMessage)msg;
			if(protocolmsg.getRequest().isInitialized())
			{
				String methodname = protocolmsg.getRequest().getMethodName();
				if(methodname.equals("HeartActive"))
				{
					com.TexasHoldem.RpcInterface.rpc.TexasHoldemService.Heart heart = TexasHoldemService.Heart.parseFrom(protocolmsg.getRequest().getRequestProto());
					if(heart.getHeartnum() == heartnum)
					{
						heartnum++;
					}
					else
					{
						ctx.channel().close();
					}
					
				}
				else
				{
					
				}
			}
			
			
			
		}
		super.channelRead(ctx, msg);
		
	}

	@Override
	protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt)
			throws Exception {
		// TODO Auto-generated method stub
		super.channelIdle(ctx, evt);
		
		if(preheartnum != heartnum)
		{
			
			TexasHoldemService.Heart.Builder data = TexasHoldemService.Heart.newBuilder();
			
			preheartnum = heartnum;
			data.setHeartnum(heartnum);
			
			BlockingRpcChannel blockchannel = RpcChannels.newBlockingRpcChannel(ctx.channel());
			
			com.TexasHoldem.RpcInterface.rpc.TexasHoldemService.TexasHoldemClient.BlockingInterface service = TexasHoldemService.TexasHoldemClient.newBlockingStub(blockchannel);
	        
	        
	        RpcController controller = new SocketRpcController();
	        try 
	        {
	       	 	Response Response = service.heartActive(controller, data.build());
	       	 
			}
	        catch (ServiceException e) 
			{
					// TODO Auto-generated catch block
	        	e.printStackTrace();
			}
		}
		else
		{
			System.out.print("Client disconnect:" + ctx.channel().toString());
			ctx.channel().close();
		}
		
	}

}
